package com.fadis.persist;

import com.fadis.FadisException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileLogProvider implements AOFLogProvider {

    private String path = null;

    public FileLogProvider(String path) {
        this.path = path;
    }

    @Override
    public void write(String cmd, String log) {
        if(path == null || "".equals(path)){
            return;
        }
        file_append_content(path, "&" + log.length() + "&" + log);
    }

    @Override
    public List<String> read() {
        List<String> list = new ArrayList<>();
        if(path == null || "".equals(path)){
            return list;
        }
        String content = file_get_content(path);

        int cmdLength = 0;
        int state = 1; // 1 长度开始，2 读长度 3 长度结束 4 读内容
        String cmd = "";
        for (int i = 0; i < content.length(); i++) {
            char c = content.charAt(i);
            if(state == 1){
                if(c != '&'){
                    throw new FadisException(9001, "长度开始需要&开始, bug get " + c + " at index " + i);
                }else{
                    cmdLength = 0;
                    cmd = "";
                }
                state = 2;
            }else if(state == 2){
                if(c == '&'){
                    state = 4;
                }else if( c < '0' || c > '9'){
                    throw new FadisException(9002, "非法数字：" + c + " at index " + i);
                }else{
                    int n = c - '0';
                    cmdLength = cmdLength * 10 + n;
                }
            }else if(state == 4){
                cmd += c;
                if(cmdLength == cmd.length()){
                    list.add(cmd);
                    state = 1;
                }
            }
        }

        return list;
    }



    public static void file_append_content(String file, String content){
        if(content == null) return;
        createFileIfNotExists(file);
        PrintWriter pw = null;
        FileOutputStream fs = null;
        try {
            fs = new FileOutputStream(file, true);
            pw = new PrintWriter(fs);
            pw.append(content);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(pw != null){
                pw.close();
            }
            if(fs != null){
                try {
                    fs.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String file_get_content(String file){
        File f = new File(file);
        if(!f.exists() || !f.isFile()) return "";
        try {
            FileInputStream fs = new FileInputStream(f);
            byte[] buffer = new byte[1024];
            int len = 0;
            StringBuffer sb =new StringBuffer();
            while((len = fs.read(buffer)) != -1){
                sb.append(new String(buffer, 0, len));
            }
            fs.close();
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean createFileIfNotExists(String file){
        File f = new File(file);
        if(f.exists()) return true;
        File dir = f.getParentFile();
        if(dir != null){
            if(!dir.exists() || !dir.isDirectory()){
                if(!dir.mkdirs()){
                    return false;
                }
            }
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }else{
            return false;
        }
    }
}
